/**  
 * All rights Reserved, Designed By www.flksh.com
 * @date:   2017-11-24 13:49:32   
 * @Copyright: 2013-2017 www.flksh.com Inc. All rights reserved. 
 * 注意：本内容仅限于上海福乐康社区养老服务有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
package com.zx.cloud.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zx.cloud.domain.ChannelSource;
import com.zx.cloud.domain.Client;
import com.zx.cloud.domain.Dict;
import com.zx.cloud.persistence.ClientMapper;
import com.zx.cloud.persistence.DictMapper;
import com.zx.cloud.persistence.LoanOrderMapper;

/**
 * 渠道数据
 */
@Service
public class ChannelService {
    private static final Logger LOGGER = LoggerFactory.getLogger(ChannelService.class);
    
    @Autowired
	private DictMapper dictMapper;
    @Autowired
    private ClientMapper clientMapper;
    @Autowired
    private LoanOrderMapper  loanOrderMapper;
    
   public   List<ChannelSource>  getChannelList(ChannelSource channelSource){
	    List<ChannelSource>  listSources=new ArrayList<ChannelSource>();
	     Dict dict=new Dict();
		 dict.setParentCode("CC0001");
		 dict.setName(channelSource.getChannelName());
		 dict.setCode(channelSource.getChannelCode());
		 List<Dict> dicts=dictMapper.getChannelByDict(dict);
		 if(dicts.size()>0){
			 for (int i = 0; i < dicts.size(); i++) {
				 String  channelCode=dicts.get(i).getCode();
				 String  channelName=dicts.get(i).getName();
				 String  channelUrl=dicts.get(i).getValue();
				 ChannelSource   cSource=new ChannelSource();
				 cSource.setChannelCode(channelCode);
				 cSource.setChannelName(channelName);
				 cSource.setChannelUrl(channelUrl);
				 /**注册量*/
				 Client    client=new Client();
				 client.setChannelCode(channelCode);
				 client.setStartTime(channelSource.getStartTime());
				 client.setEndTime(channelSource.getEndTime());
				 Long   registerCount=clientMapper.getClientToChannel(client);
				 cSource.setRegisterCount(registerCount);
				 /**借款量*/
				 Map<String, Object> param=new HashMap<String, Object>();
				 param.put("channelCode", channelCode);
				 param.put("startTime", channelSource.getStartTime());
				 param.put("endTime", channelSource.getEndTime());
				 long loanCount=loanOrderMapper.getLoanToChannel(param);
				 cSource.setLoanCount(loanCount);
				 /**通过量*/
				 param.put("status", 2);
				 long passCount=loanOrderMapper.getLoanToChannel(param);
				 cSource.setPassCount(passCount);
				 listSources.add(cSource);
			}
		 }
		 
	   return  listSources;
   }
}
